using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;

namespace Microsoft.MSE
{
    public enum MseEvents
    {
        None = 0,
        MseMethodStart = 1,
        MseMethodEnd = 2,
        MseMessageTransformBegin = 3,
        MseMessageTransformEnd = 4
    }
    public static class TraceHelper
    {
        static TraceSource m_TraceSource;

        [DebuggerStepThrough]
        public static void InitializeTraceSource(string sourceName)
        {
            try
            {
                m_TraceSource = new TraceSource(sourceName);

            }
            catch (Exception ex)
            {
                EventLog.WriteEntry(MseConstants.EventLogName, "Exception initializing TraceSource: " + ex.Message);
            }
        }

        public static TraceSource MseTraceSource
        {
            get { return m_TraceSource; }
        }

        [DebuggerStepThrough]
        public static void TraceBeginMethod(string p)
        {
            if (null != m_TraceSource)
            {
                m_TraceSource.TraceEvent(TraceEventType.Start,
                                    (int)MseEvents.MseMethodStart,
                                    p);
                m_TraceSource.Flush();
            }
        }

        [DebuggerStepThrough]
        public static void TraceEndMethod(string p)
        {
            if (null != m_TraceSource)
            {
                m_TraceSource.TraceEvent(TraceEventType.Stop,
                                    (int)MseEvents.MseMethodEnd,
                                    p);
                m_TraceSource.Flush();
            }
        }

        [DebuggerStepThrough]
        public static void TraceWarning(string p)
        {
            if (null != m_TraceSource)
            {
                m_TraceSource.TraceEvent(TraceEventType.Warning,
                                    (int)MseEvents.None,
                                    p);
                m_TraceSource.Flush();
            }
        }

        [DebuggerStepThrough]
        public static void TraceWarning(string p, params object[] args)
        {
            if (null != m_TraceSource)
            {
                m_TraceSource.TraceEvent(TraceEventType.Warning,
                                    (int)MseEvents.None,
                                    p, args);
                m_TraceSource.Flush();
            }
        }

        [DebuggerStepThrough]
        public static void TraceInformation(string p)
        {
            if (null != m_TraceSource)
            {
                m_TraceSource.TraceInformation(p);
                m_TraceSource.Flush();
            }
        }

        [DebuggerStepThrough]
        public static void TraceInformation(string p, params object[] args)
        {
            if (null != m_TraceSource)
            {
                m_TraceSource.TraceInformation(p, args);
                m_TraceSource.Flush();
            }
        }

        [DebuggerStepThrough]
        public static void TraceError(string exmsg)
        {
            if (null != m_TraceSource)
            {
                m_TraceSource.TraceEvent(TraceEventType.Error,
                                    (int)MseEvents.None,
                                    exmsg);
                m_TraceSource.Flush();
            }
        }

        [DebuggerStepThrough]
        public static void TraceError(string exmsg, params object[] args)
        {
            if (null != m_TraceSource)
            {
                m_TraceSource.TraceEvent(TraceEventType.Error,
                                    (int)MseEvents.None,
                                    exmsg, args);
                m_TraceSource.Flush();
            }
        }

        // NZ Code Added
        // This function builds a string based upon descriptions from all
        // the exceptions in the chain
        [DebuggerStepThrough]
        public static string GetExceptionDetails(Exception ex)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("Exception Information Type[{0}] Source[{1}] Message[{2}] StackTrace[{3}]", ex.GetType().Name,ex.Source, ex.Message,ex.StackTrace);
            Exception iex = ex.InnerException;
            while (iex != null)
            {
                sb.AppendFormat("Exception Information Type[{0}] Source[{1}] Message[{2}] StackTrace[{3}]", iex.GetType().Name, iex.Source, iex.Message, iex.StackTrace);
                iex = iex.InnerException;
            }
            return sb.ToString();
        }
    }
}
